package com.Lubaichen.Wiki.controller;

import com.Lubaichen.Wiki.req.CategoryQueryReq;
import com.Lubaichen.Wiki.req.CategorySaveReq;
import com.Lubaichen.Wiki.resp.CommonResp;
import com.Lubaichen.Wiki.resp.CategoryQueryResp;
import com.Lubaichen.Wiki.resp.PageResp;
import com.Lubaichen.Wiki.service.CategoryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author Lubaichen
 * @date 2022年03月19日 19:52
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
    @Resource
    private CategoryService categoryService;

    @GetMapping("/list") //Spring会自动将参数映射到类属性
    public CommonResp categoryList(@Valid CategoryQueryReq req) {
        CommonResp<PageResp<CategoryQueryResp>> resp = new CommonResp<>();
        PageResp<CategoryQueryResp> list = categoryService.list(req);
        resp.setContent(list);
        return resp;
    }

    @GetMapping("/all")
    public CommonResp categoryAll(CategoryQueryReq req) {
        CommonResp<List<CategoryQueryResp>> commonResp = new CommonResp<>();
        List<CategoryQueryResp> all = categoryService.all(req);
        commonResp.setContent(all);
        return commonResp;
    }


    @PostMapping("/save")
    public CommonResp categorySave(@Valid @RequestBody CategorySaveReq req){
        CommonResp resp = new CommonResp<>();
        categoryService.save(req);
        return resp;
    }

    @DeleteMapping("/delete/{id}")
    public CommonResp deleteEbookById(@PathVariable Long id) {
        CommonResp resp = new CommonResp<>();
        categoryService.deleteEbookById(id);
        return resp;
    }

    @GetMapping("/search")
    public CommonResp categorySearch(CategoryQueryReq req) {
        CommonResp<List<CategoryQueryResp>> commonResp = new CommonResp<>();
        List<CategoryQueryResp> list = categoryService.search(req);
        commonResp.setContent(list);
        return commonResp;
    }
}
